<?php
/**
 * WB_CacheManager
 * Shared memory cache.
 *
 * Note that the method 'clearCache' does _not_
 * perform unconditional clearing of the cache.
 *
 * @author Jean-Lou Dupont
 * @version @@package-version@@
 * @Id $Id: CacheManager.php 94 2008-03-06 20:13:11Z jldupont $
 */

//{{include
require_once 'web-bloks/BaseClass/BaseClass.php';
require_once 'web-bloks/Singleton/Singleton.php';
require_once 'web-bloks/CacheClass/CacheClass.php';
#require_once 'web-bloks/ObjFactory/ObjFactory.php';
//include}} 

class WB_CacheManager
	extends WB_BaseClass
	implements WB_Singleton_IF
{
	/**
	 * @private
	 */
	static $instance = null;
	
	/**
	 * Array sorted in order of preference
	 * @private
	 */
	static $_classes = array(
	);

	/**
	 * Cache currently active
	 * @private
	 */
	static $_cache = null;

	/**
	 * Can't create an instance of this class directly;
	 * need to go through the singleton interface
	 * provided by the WB_ObjFactory base class.
	 *
	 */
	public function __construct( $params = null )
	{
		self::init();
		parent::__construct( $params );		
	}

	// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	// WB_BaseClass
	// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%	
	public function getClassPath()
	{
		return 'web-bloks/CacheManager';
	}
	public function getClassVersion()
	{
		return '$Id: CacheManager.php 94 2008-03-06 20:13:11Z jldupont $';
	}
	// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	// Singleton Interface
	// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%	
	public static function getSingleton( $params = null )	
	{
		return self::_handleSingleton( self::$instance, 'WB_CacheManager', $params );
	}
	// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	// Interface
	// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%	
	
	/**
	 * Registers a cache class with this manager
	 * 
	 * @param $classe Object
	 */
	public static function register( $classe )
	{
		self::$_classes[] = $classe;	
	}
	/**
	 * isFake
	 * 
	 * @return boolean
	 */
	public static function isFake()
	{
		// just in case.
		if ( !is_object( self::$_cache ))
			return null;
		
		// default 'fake' cache
		if (self::$_cache instanceof WB_CacheFake)
			return true;
		
		// lastly
		$obj = self::$_cache;
				
		return $obj->isFake();
	}
	protected static function init()
	{
		foreach ( self::$_classes as $classe )
			if (self::checkPresence( $classe ))
			{
				$cache = WB_ObjFactory::factory( $classe );
				
				if ( !$cache->isFake() )
				{
					self::$_cache = $cache;
					break;
				}
			}
			
		// didn't find any 'real' cache...
		// configure our default 'fake' one.
		if ( is_null( self::$_cache ) )
			self::$_cache = new WB_CacheFake;			
	}
	/**
	 * Returns the active cache
	 * 
	 * @return object
	 */
	public static function getCache()
	{
		return self::$_cache;
	}
	/**
	 * Returns a list of available caches
	 * 
	 * @return array
	 */
	public static function getAvailableCaches()
	{
		$caches = array();
		
		foreach ( self::$_classes as $classe )
			if ( self::checkPresence( $classe ) )
				array_push( $caches, $classe );

		return $caches;
	}
	/**
	 * Verifies if a given cache class is available
	 * 
	 * @return boolean
	 * @param $classe string
	 */
	public static function checkPresence( &$classe )
	{
		if ( !class_exists( $classe ) )
			return false;
			
		$c = " return $classe::checkPresence(); ";	
		return eval( $c );
	}
	
}// end WB_Cache_Manager

// Not very pretty but more efficient!
//{{include
require_once 'web-bloks/CacheManager/Fake.php';
//include}} 
